package com.hui.account.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hui.account.R;
import com.hui.account.entity.RecordEntity;
import com.hui.account.utils.CommonUtils;
import com.hui.account.utils.TimeUtils;

import java.util.List;

/**
 * 客户欠款明细适配器
 */
public class UserRecordAdapter extends BaseAdapter {

    Context context;

    List<RecordEntity> data; // 欠款记录

    LayoutInflater inflater; // 获取视图

    public UserRecordAdapter(Context context, List<RecordEntity> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 获取视图
     */
    @SuppressLint({"ViewHolder", "SetTextI18n"})
    @Override
    public View getView(int i, View view, ViewGroup parent) {
        // 1、获取视图
        view = inflater.inflate(R.layout.item_user_record, parent, false);
        ViewHolderUserRecord entity = new ViewHolderUserRecord(view);
        view.setTag(entity);

        // 2、填充数据
        RecordEntity r = data.get(i);
        entity.timeText.setText(TimeUtils.getStringToTimestamp(r.getCreateTime(), TimeUtils.TIME_SQL_PATTERN_TIME));
        entity.moneyText.setText(CommonUtils.MONEY + r.getMoney());

        return view;
    }

    /**
     * 欠款记录视图对应的实体类
     */
    static class ViewHolderUserRecord {

        TextView timeText, moneyText;

        public ViewHolderUserRecord(View view) {
            timeText = view.findViewById(R.id.item_user_record_time);
            moneyText = view.findViewById(R.id.item_user_record_money);
        }
    }
}
